package system

import (
	"cubeAdmin/common"
	"cubeAdmin/common/jwtauth"
	"cubeAdmin/controllers"
	"cubeAdmin/dto"
	"cubeAdmin/models"
	"github.com/jinzhu/copier"
	"strconv"
)

// SysConfigDetailController operations for SysConfigDetail
type SysConfigDetailController struct {
	controllers.BaseController
}

// URLMapping ...
func (c *SysConfigDetailController) URLMapping() {
	c.Mapping("Add", c.Add)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Post", c.Update)
	c.Mapping("Delete", c.Delete)
}

// Add ...
// @Title Post
// @Description create SysConfigDetail
// @Param	body 	models.SysConfigDetail	true		"body for SysConfigDetail content"
// @Success 201 {int} models.SysConfigDetail
// @Failure 403 body is empty
// @router /add [post]
func (c *SysConfigDetailController) Add() {

	var req dto.ConfigDetailAddReq
	//valid := validation.Validation{}
	if err := c.ParseForm(&req); err != nil {
		c.Fail(err.Error(), common.MsgErr)
	}

	//err := json.Unmarshal(c.Ctx.Input.RequestBody, &configDetail)
	//if err != nil {
	//	c.Fail(err.Error(), common.UnmarshalErr)
	//}
	//b, _ := valid.Valid(&configDetail)
	//if !b {
	//	for _, err := range valid.Errors {
	//		c.Fail(err.Message, common.ArgsValidErr)
	//	}
	//}

	var configDetail models.SysConfigDetail
	if err := copier.Copy(&configDetail, &req); err != nil {
		c.Fail(err.Error(), common.ConvertErr)
	}

	id, _ := jwtauth.GetAdminUserId(c.Ctx.Input)
	configDetail.CreateBy = id
	configDetail.UpdateBy = id
	_, e := models.AddSysConfigDetail(&configDetail)
	if e != nil {
		c.Fail(e.Error(), common.DatabaseOperationErr)
	}
	c.Ok("操作成功")
}

// GetOne ...
// @Title Get One
// @Description get SysConfigDetail by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.SysConfigDetail
// @Failure 403 :id is empty
// @router /:id [get]
func (c *SysConfigDetailController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v, err := models.GetSysConfigDetailById(id)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll ...
// @Title GetAll
// @Description get SysConfig
// @Param	configId	query	string	false	"字典Id"
// @Param	name	query	string	false	"配置标题"
// @Success 200 {object} dada.ResultTable
// @Failure 403
// @router / [get]
func (c *SysConfigDetailController) GetAll() {
	// 查询对象
	var req dto.ConfigDetailPageReq
	// 参数绑定
	if err := c.ParseForm(&req); err != nil {
		c.Fail(err.Error(), common.ParseFormErr)
	}
	// 调用查询方法
	list, count, err := new(models.SysConfigDetail).GetList(req)
	if err != nil {
		c.Fail(err.Error(), common.DatabaseOperationErr)
	}
	// 返回结果
	c.PageTable(count, list)
}

// Update ...
// @Title Put
// @Description update the SysConfigDetail
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body 	models.SysConfigDetail	true		"body for SysConfigDetail content"
// @Success 200 {object} models.SysConfigDetail
// @Failure 403 :id is not int
// @router /:id [post]
func (c *SysConfigDetailController) Update() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v := models.SysConfigDetail{Id: id}
	if err := c.ParseForm(&v); err == nil {
		uId, _ := jwtauth.GetAdminUserId(c.Ctx.Input)
		v.UpdateBy = uId
		if err := models.UpdateSysConfigDetailById(&v); err == nil {
			c.Ok("操作成功")
		} else {
			c.Fail(err.Error(), common.DatabaseOperationErr)
		}
	} else {
		c.Fail(err.Error(), common.ParseFormErr)
	}
}

// Delete ...
// @Title Delete
// @Description delete the SysConfigDetail
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *SysConfigDetailController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	if err := models.DeleteSysConfigDetail(id); err == nil {
		c.Ok("删除成功")
	} else {
		c.Fail(err.Error(), common.DatabaseOperationErr)
	}
}
